package com.kylin.system.controller;

import com.kylin.common.core.annotation.JSONSerialized;
import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.core.validator.BeanValidator;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.common.security.annotation.NoLogin;
import com.kylin.system.entity.SysConfig;
import com.kylin.system.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 参数配置 信息操作处理
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/config")
public class SysConfigController
{
    @Autowired
    private SysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysConfig>> list(SysConfig config)
    {
        // 构造搜索
        config.getQueryWrapper()
                .eq(StringUtils.isNotBlank(config.getConfigGroup()),"config_group", config.getConfigGroup())
                .like(StringUtils.isNotBlank(config.getConfigName()), "config_name", config.getConfigName())
                .eq(StringUtils.isNotBlank(config.getConfigType()), "config_type", config.getConfigType());

        Page<SysConfig> page = new Page<>();
        page = configService.listPage(page, config);
        return ResultBean.onSuccess(page);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public ResultBean<SysConfig> getInfo(@PathVariable Long configId)
    {
        // 非空校验
        Assert.isBlank("查询编号不能为空", configId);
        return ResultBean.onSuccess(configService.getById(configId));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
//    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultBean<Serializable> add(@RequestBody SysConfig config)
    {
        // 参数校验
        BeanValidator.validate(config);
        configService.save(config);
        return ResultBean.onSuccess();
    }

    /**
     * 修改参数配置
     */
//    @PreAuthorize("@ss.hasPermi('system:config:edit')")
//    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean<Serializable> edit(@RequestBody SysConfig config)
    {
        // 参数校验
        BeanValidator.validate(config);
        configService.save(config);
        return ResultBean.onSuccess();
    }

    /**
     * 根据id更新配置值
     * @author wuhao
     * @version 1.0.0 - 2023/10/31
     */
    @PostMapping("update/value")
    public ResultBean<Serializable> updateValue(@RequestBody SysConfig config)
    {
        Assert.isBlank("缺少参数", config.getId());
        configService.save(config);
        return ResultBean.onSuccess();
    }

    /**
     * 删除参数配置
     */
//    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public ResultBean<Object> remove(@PathVariable Long[] configIds)
    {
        Assert.isBlank("请选择需要删除的数据" , configIds);
        int count = configService.deleteBatchIds(Arrays.asList(configIds));
        return ResultBean.onSuccess().put("count", count);
    }


    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public ResultBean<Serializable> getConfigKey(@PathVariable String configKey)
    {
        HashMap<String,String> data = new HashMap<>();
        data.put(configKey, configService.selectConfigByKey(configKey));
        return ResultBean.onSuccess(data);
    }

    /**
     * 根据分组查询所有配置
     */
    @GetMapping(value = "/configGroup/{group}")
    public ResultBean<List<SysConfig>> getConfigGroup(@PathVariable String group)
    {
        return ResultBean.onSuccess(configService.selectConfigByGroup(group));
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public ResultBean<Serializable> clearCache()
    {
        configService.clearCache();
        return ResultBean.onSuccess();
    }

    /**
     * 获取非系统内置配置列表
     */
    @GetMapping(value = "/custom")
    @NoLogin("/config/custom")
    @JSONSerialized(includes = {"configKey", "configValue"}, clazz = SysConfig.class)
    public ResultBean<List<SysConfig>> getCustomConfig()
    {
        return ResultBean.onSuccess(configService.getCustomConfigList());
    }

}
